package com.fizzkai.p2p.mgrsite.controller;

import com.fizzkai.p2p.bussness.domain.BidRequest;
import com.fizzkai.p2p.bussness.domain.BidRequestAuditHistory;
import com.fizzkai.p2p.bussness.query.BidRequestQuery;
import com.fizzkai.p2p.bussness.service.IBidRequestAuditHistoryService;
import com.fizzkai.p2p.bussness.service.IBidRequestService;
import com.fizzkai.p2p.core.domain.UserInfo;
import com.fizzkai.p2p.core.service.IRealAuthService;
import com.fizzkai.p2p.core.service.IUserInfoService;
import com.fizzkai.p2p.core.util.AjaxResult;
import com.fizzkai.p2p.core.util.Constants;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;
import java.util.List;

/**
 * 后台 借款单 Controller
 */
@Controller
public class BidRequestController {

    @Autowired
    private IBidRequestService bidRequestService;
    @Autowired
    private IUserInfoService userInfoService;
    @Autowired
    private IRealAuthService realAuthService;
    @Autowired
    private IBidRequestAuditHistoryService historyService;


    /**
     * 发标前 审核列表
     * @param qo
     * @param model
     * @return
     */
    @RequestMapping("/bidrequest_publishaudit_list")
    public String bidrequest_publishaudit_list(@ModelAttribute("qo") BidRequestQuery qo, Model model){
        qo.setBidRequestState(Constants.BIDREQUEST_STATE_APPLY);
        PageInfo<BidRequest> pageInfo = bidRequestService.query(qo);
        model.addAttribute("pageInfo", pageInfo);
        return "bidrequest/publish_audit";
    }

    /**
     * 发标前 审核
     * @param id
     * @param state
     * @param publishTime
     * @param remark
     * @return
     */
    @RequestMapping("/bidrequest_publishaudit")
    @ResponseBody
    public Object bidrequest_publishaudit(Long id, Integer state,
        @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") Date publishTime, String remark){
        bidRequestService.publishAudit(id, state, publishTime, remark);
        return new AjaxResult("操作成功");
    }

    // 借款 详情页面
    @RequestMapping("/borrow_info")
    public String borrow_info(Long id, Model model){
        // bidRequest
        BidRequest bidRequest = bidRequestService.get(id);
        model.addAttribute("bidRequest", bidRequest);
        // audits 审核 记录
        List<BidRequestAuditHistory> audits = historyService.queryListByRequestId(bidRequest.getId());
        model.addAttribute("audits", audits);
        if (bidRequest.getBidRequestType() == Constants.BIDREQUEST_TYPE_NORMAL) {
            // 正常标
            // userInfo 借款人的 createUser
            UserInfo userInfo = userInfoService.get(bidRequest.getCreateUser().getId());
            model.addAttribute("userInfo", userInfo);

            // realAuth 借款人 信息
            model.addAttribute("realAuth", realAuthService.get(userInfo.getRealAuthId()));
            return "bidrequest/borrow_info";
        }
        // 体验标
        return "expbidrequest/borrow_info";
    }

    /**
     * 满标一审 列表
     * @param qo
     * @param model
     * @return
     */
    @RequestMapping("/bidrequest_audit1_list")
    public String bidrequest_audit1_list(@ModelAttribute("qo") BidRequestQuery qo, Model model){
        qo.setBidRequestState(Constants.BIDREQUEST_STATE_APPROVE_PENDING_1);
        qo.setStates(null);
        PageInfo<BidRequest> pageInfo = bidRequestService.query(qo);
        model.addAttribute("pageInfo", pageInfo);
        return "bidrequest/audit1";
    }

    /**
     * 满标一审 审核
     *
     * @return
     */
    @RequestMapping("/bidrequest_audit1")
    @ResponseBody
    public Object bidrequest_audit1(Long id, Integer state, String remark) {

        bidRequestService.audit1(id, state, remark);
        return new AjaxResult("审核成功");
    }

    /**
     * 满标 二审列表
     * @param model
     * @return
     */
    @RequestMapping("/bidrequest_audit2_list")
    public String bidrequest_audit2_list(@ModelAttribute("qo") BidRequestQuery qo, Model model){
        qo.setBidRequestState(Constants.BIDREQUEST_STATE_APPROVE_PENDING_2);
        qo.setStates(null);
        PageInfo<BidRequest> pageInfo = bidRequestService.query(qo);
        model.addAttribute("pageInfo", pageInfo);
        return "bidrequest/audit2";
    }

    /**
     * 满标二审 请求
     * @return
     */
    @RequestMapping("/bidrequest_audit2")
    @ResponseBody
    public Object bidrequest_audit2(Long id, Integer state, String remark){
        bidRequestService.audit2(id, state, remark);
        return new AjaxResult("审核成功");
    }
}
